﻿using Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Api
{
    /// <summary>
    /// 用户行为日志表
    /// </summary>
    public class BehaviorLogService : BaseService<BehaviorLog>
    {

        private static BehaviorLogService instance;
        private static object _lock = new object();
        private BehaviorLogService() { }

        /// <summary>
        /// 获取实体类
        /// </summary>
        /// <returns></returns>
        public static BehaviorLogService getInstance()
        {
            if (instance == null)
            {
                lock (_lock)
                {
                    if (instance == null)
                    {
                        instance = new BehaviorLogService();
                        return instance;
                    }
                }
            }
            return instance;
        }
        /// <summary>
        ///  新增行为 
        /// </summary>
        /// <param name="behavior_type">1进场刷卡，2进场刷指纹，3存物刷卡，4取物刷卡，5出场刷卡</param>
        /// <param name="behavior_status">行为成功与否 1 成功  ，2失败</param>
        /// <param name="error_msg">错误信息</param>
        /// <param name="card_no">卡号</param>
        /// <param name="lockers_no">储物柜编号</param>
        /// <param name="brakemachine_no">闸机编号</param>
        /// <returns></returns>
        /// <summary>
        public void addBehavior(int behavior_type, int behavior_status, string error_msg, string card_no, string lockers_no, string brakemachine_no)
        {
            if (card_no == null || card_no == "")
            {
                throw new ArgumentNullException("card_no 为空");
            }
            var behaviorLog = new BehaviorLog
            {
                behavior_type = behavior_type,
                behavior_status = behavior_status,
                error_msg = error_msg,
                card_no = card_no,
                lockers_no = lockers_no,
                brakemachine_no = brakemachine_no
            };

            save(behaviorLog);
        }
        /// <summary>
        /// 进闸机 日志记录 
        /// </summary>
        /// <param name="card_no">卡号</param>
        /// <param name="machine_no">闸机编号</param>
        /// <param name="behavior_status">行为成功与否</param>
        /// <param name="error">错误信息</param>
        public void addInBrackmachineBehavior(string card_no, string brakemachine_no, int behavior_status, string error)
        {
            if (card_no == null || card_no == "" || brakemachine_no == null || brakemachine_no == "")
            {
                throw new ArgumentNullException("card_no 为空,或者 brakemachine_no 为空");
            }
            Console.WriteLine("behavior_status:" + behavior_status);
            string in_out_code = null;//进场时候的唯一标识
            if (behavior_status == 1)//成功
            {
                in_out_code = Guid.NewGuid().ToString("N");
            }
            Console.WriteLine("in_out_code:" + in_out_code);
            var behaviorLog = new BehaviorLog
            {
                behavior_type = 1,
                behavior_status = behavior_status,
                error_msg = error,
                card_no = card_no,
                brakemachine_no = brakemachine_no,
                in_out_code = in_out_code
            };
            int temp= save(behaviorLog);
          //  Console.WriteLine("temp:"+temp + "");

        }
        /// <summary>
        /// 设置免闸机验证
        /// </summary>
        /// <param name="card_no">卡号</param>
        /// <param name="machine_no">闸机编号</param>
        /// <param name="behavior_status">行为成功与否</param>
        /// <param name="error">错误信息</param>
        public void setFingerFree(string card_no)
        {
            updateBySql("update m_interface set free_finger_varify=0 where card_no='" + card_no + "'");
         }


        /// <summary>
        /// 出闸机 日志记录 
        /// </summary>
        /// <param name="card_no">卡号</param>
        /// <param name="machine_no">闸机编号</param>
        /// <param name="behavior_status">行为成功与否</param>
        /// <param name="error">错误信息</param>
        public void addOutBrackmachineBehavior(string card_no, string brakemachine_no, int behavior_status, string error)
        {
            if (card_no == null || card_no == "" || brakemachine_no == null || brakemachine_no == "")
            {
                throw new ArgumentNullException("card_no 为空,或者 brakemachine_no 为空");
            }
           
            string in_out_code = null;
            var behaviorLog = new BehaviorLog
            {
                behavior_type = 5,
                behavior_status = behavior_status,
                error_msg = error,
                card_no = card_no,
                brakemachine_no = brakemachine_no,
                in_out_code = in_out_code
            };

            if (behavior_status == 1)//成功
            {   
                string in_out_code_sql_2 = "select in_out_code from m_behavior_log where card_no=? and behavior_type=1 and behavior_status=1 order by id desc limit 1";
                object obj_2 = uniqResult(in_out_code_sql_2, new object[] { card_no });
                if (obj_2 != null && !("".Equals(obj_2.ToString().Trim())))
                {
                    behaviorLog.in_out_code = obj_2.ToString().Trim();//通过第一种方式获取code 值
                    save(behaviorLog);
                    return;
                }
                behaviorLog.in_out_code = card_no;
                behaviorLog.error_msg = "100011";
                save(behaviorLog);
                return;
            }
            save(behaviorLog);
        }
        /// <summary>
        ///存物 日志记录 
        /// </summary>
        /// <param name="card_no">卡号</param>
        /// <param name="lockers_no">储物柜编号</param>
        /// <param name="behavior_status">行为成功与否</param>
        /// <param name="error">错误信息</param>
        public void addLockersBehavior(string card_no, string lockers_no, int behavior_status, string error)
        {
            if (card_no == null || card_no == "" || lockers_no == null || lockers_no == "")
            {
                throw new ArgumentNullException("card_no 为空 或者 lockers_no 为空");
            }
            var behaviorLog = new BehaviorLog
            {
                behavior_type = 3,
                behavior_status = behavior_status,
                error_msg = error,
                card_no = card_no,
                lockers_no = lockers_no
            };
            save(behaviorLog);
        }

        /// <summary>
        ///取物 日志记录 
        /// </summary>
        /// <param name="card_no">卡号</param>
        /// <param name="lockers_no">储物柜编号</param>
        /// <param name="behavior_status">行为成功与否</param>
        /// <param name="error">错误信息</param>
        public void addUnLockersBehavior(string card_no, string lockers_no, int behavior_status, string error)
        {
            if (card_no == null || card_no == "")
            {
                throw new ArgumentNullException("card_no 为空");
            }
            var behaviorLog = new BehaviorLog
            {
                behavior_type = 4,
                behavior_status = behavior_status,
                error_msg = error,
                card_no = card_no,
                brakemachine_no = lockers_no
            };
            save(behaviorLog);
        }

        public int owner_type(string card_no)
        {  
                //判断是否为员工卡
            int owner_type = get_owner_type(String.Format("select owner_type from m_card where card_no='{0}'",card_no));
            if (owner_type ==1 )
            {
                //会员
                return 1;

            }
            else if (owner_type == 2)
            {
                //员工
                return 2;
            }
            else { 
                //既不是员工又不是会员
                return 0;
            }
        }

        /// <summary>
        ///刷指纹 日志记录 
        /// </summary>
        /// <param name="card_no">卡号</param>
        /// <param name="behavior_status">行为成功与否</param>
        /// <param name="error">错误信息</param>
        public void addSwipeFingerBehavior(string card_no, int behavior_status, string error, string brakemachine_no)
        {
            if (card_no == null || card_no == "")
            {
                throw new ArgumentNullException("card_no 为空");
            }
            var behaviorLog = new BehaviorLog
            {
                behavior_type = 2,
                behavior_status = behavior_status,
                error_msg = error,
                card_no = card_no,
                brakemachine_no = brakemachine_no
            };
            save(behaviorLog);
        }

    }
}
